// pages/sensorData/sensorData.js
const app = getApp()
import * as echarts from '../../ec-canvas/echarts'

Page({

  /**
   * 页面的初始数据
   */

  data: {
    data:[1,2],
    soil_tem1:[1,2,3,4,5,6,7],
    soil_tem2:[],
    siol_hum1:[],
    siol_hum2:[],
    time:[],
    imagePath:'',
    err:'错误错误错误错误错误',

    ec: {
      onInit: initChart
    },
    ec2:{
      onInit: initChart2
    }
  },

  // 获取华为云实时数据
  getData(){
    // setInterval(() => {
    // }, 3000);
    wx.request({
      url: app.globalData.serverPort+'/sensor/getDevicesData',
      data:{
      },
      success:(res)=>{
        // console.log(res.data.data.shadow[0].reported.properties.Soil_Temperature1)
        this.setData({
          data:res.data.data.shadow[0].reported.properties
        })

        console.log(this.data.data)
      },
      fail:(res)=>{
        // 请求失败，获取错误信息
        console.log(res.errMsg);
        this.setData({
          err: res.errMsg
        })
      }
    })
  },
  // 获取折线图的数据
  getData2(){
    // 获取土温1
    wx.request({
      url: app.globalData.serverPort+'/sensor/getNumPropertes?type=soil_tem&sen_id=1&limit=7',
      data:{},
      success:(res)=>{
        let tem = res.data.data
        let soil_tem = [1,2,3,4,5,6,7]
        let time = [0,0,0,0,0,0,0]
        for(let i = 0;i<res.data.data.length;i++){
          soil_tem[i] = res.data.data[i].data
          time[i] = (String)(res.data.data[i].time).substring(11,16)
        }
        app.globalData.soil_tem1 = soil_tem
        app.globalData.time = time
      },
      fail:(res)=>{
        // 请求失败，获取错误信息
        console.log(res.errMsg);
        this.setData({
          err: res.errMsg
        })
      }
    })
    // 获取土温2
    wx.request({
      url: app.globalData.serverPort+'/sensor/getNumPropertes?type=soil_tem&sen_id=2&limit=7',
      data:{},
      success:(res)=>{
        let soil_tem = [1,2,3,4,5,6,7]
        let time = [0,0,0,0,0,0,0]
        for(let i = 0;i<res.data.data.length;i++){
          soil_tem[i] = res.data.data[i].data
        }
        app.globalData.soil_tem2 = soil_tem
      },
      fail:(res)=>{
        // 请求失败，获取错误信息
        console.log(res.errMsg);
        this.setData({
          err: res.errMsg
        })
      }
    })
    // 获取土湿度1
    wx.request({
      url: app.globalData.serverPort+'/sensor/getNumPropertes?type=soil_moi&sen_id=1&limit=7',
      data:{},
      success:(res)=>{
        let soil_hum = [1,2,3,4,5,6,7]
        let time = [0,0,0,0,0,0,0]
        for(let i = 0;i<res.data.data.length;i++){
          soil_hum[i] = res.data.data[i].data
        }
        app.globalData.soil_hum1 = soil_hum
      },
      fail:(res)=>{
        // 请求失败，获取错误信息
        console.log(res.errMsg);
        this.setData({
          err: res.errMsg
        })
      }
    })

    // 获取土湿度2
    wx.request({
      url: app.globalData.serverPort+'/sensor/getNumPropertes?type=soil_moi&sen_id=2&limit=7',
      data:{},
      success:(res)=>{
        let soil_hum = [1,2,3,4,5,6,7]
        let time = [0,0,0,0,0,0,0]
        for(let i = 0;i<res.data.data.length;i++){
          soil_hum[i] = res.data.data[i].data
        }
        app.globalData.soil_hum2 = soil_hum
      },
      fail:(res)=>{
        // 请求失败，获取错误信息
        console.log(res.errMsg);
        this.setData({
          err: res.errMsg
        })
      }
    })
  },



  // 选择相册图片
  chooseImg(){
    wx.chooseMedia({
      success:(res)=>{
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData()
    this.getData2()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})

// 土壤温度传感器
function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // 像素比
  });
  canvas.setChart(chart);

  var option = {
    title: {
      text: '土壤温度'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['soil_tem1', 'soil_tem2']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    toolbox: {
      // feature: {
      //   saveAsImage: {}
      // }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: app.globalData.time
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: 'soil_tem1',
        type: 'line',
        stack: 'Total',
        data: app.globalData.soil_tem1
      },
      {
        name: 'soil_tem2',
        type: 'line',
        stack: 'Total',
        data: app.globalData.soil_tem2
      }
    ]
  };
  chart.setOption(option);
  return chart;
}

// 土壤湿度传感器
function initChart2(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // 像素比
  });
  canvas.setChart(chart);

  var option = {
    title: {
      text: '土壤湿度'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['soil_tem1', 'soil_tem2']
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    toolbox: {
      // feature: {
      //   saveAsImage: {}
      // }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: app.globalData.time
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: 'soil_tem1',
        type: 'line',
        stack: 'Total',
        data: app.globalData.soil_hum1
      },
      {
        name: 'soil_tem2',
        type: 'line',
        stack: 'Total',
        data: app.globalData.soil_hum2
      }
    ]
  };
  chart.setOption(option);
  return chart;
}